<?php
//include 'db.php';
class Talker{
    private $session_a;
    private $session_b;
    private $a_sta;
    private $b_sta;
    private $contenet;
    private $db;
    private $table_name;

    function __construct()
    {
	$this->db = new Db;
	$this->db->db_connect();
	$this->table_name = 'talk_table';
    }
    public function init($session_a,$session_b)
    {
	$this->session_a = $session_a;
	$this->session_b = $session_b;
	$this->a_sta = 0;
	$this->b_sta = 0;
    }
    public function insert()
    {
	$this->db->db_insert($this->table_name,array(
	    'session_a'=>$this->session_a,
	    'a_sta'=>$this->a_sta,
	    'session_b'=>$this->session_b,
	    'b_sta'=>$this->b_sta,
	    'content'=>''
	)) or die("添加不成功");
    }
    public function get_other_session_id($session_id)
    {
	$result = $this->db->db_select($this->table_name,array('*'),'session_a=\''.$session_id.'\' or session_b=\''.$session_id.'\'');
	$rows = mysql_num_rows($result);
	if($rows == 0)
	    return FALSE;
	$row = mysql_fetch_array($result);
	if($row['session_a'] == $session_id)
	    return $row['session_b'];
	else
	    return $row['session_a'];
    }
    public function get_other_sta($session_id)
    {
	$result = $this->db->db_select($this->table_name,array('*'),'session_a=\''.$session_id.'\' or session_b=\''.$session_id.'\'');
	$rows = mysql_num_rows($result);
	if($rows == 0)
	    return FALSE;
	$row = mysql_fetch_array($result);
	if($row['session_a'] == $session_id)
	    return $row['b_sta'];
	else
	    return $row['a_sta'];
    }
    public function get_msg($session_id)
    {
	$result = $this->db->db_select($this->table_name,array('*'),'session_a=\''.$session_id.'\' or session_b=\''.$session_id.'\'');
	$rows = mysql_num_rows($result);
	if($rows == 0)
	    return FALSE;
	$row = mysql_fetch_array($result);
	return $row['content'];
    }
    public function send_msg($session_id,$msg)
    {
	$msg = '\''.$msg.'\'';
	
	$this->db->db_update($this->table_name,'content',$msg,'session_a = \''.$session_id.'\' or session_b=\''.$session_id.'\'');
	$this->db->db_update($this->table_name,'a_sta',1,'session_a = \''.$session_id.'\'');
	$this->db->db_update($this->table_name,'b_sta',1,'session_b = \''.$session_id.'\'');
    }
    public function set_other_sta($session_id)
    {
	$this->db->db_update($this->table_name,'b_sta',0,'session_a = \''.$session_id.'\'');
	$this->db->db_update($this->table_name,'a_sta',0,'session_b = \''.$session_id.'\'');
    }
    public function del()
    {
	$this->db->db_delete($this->table_name,'session_a=\''.$this->session_a.'\' and session_b=\''.$this->session_b.'\' or session_a=\''.$this->session_b.'\' and session_b=\''.$this->session_a.'\'');
    }
}

?>
